package uk.co.q3c.deplan.client.domain.task;

import java.util.LinkedList;
import java.util.logging.Logger;

import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;
import uk.co.q3c.deplan.client.domain.resource.Resource;
import uk.co.q3c.deplan.client.domain.util.DailyProfile_LinkedList;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Serialized;
import com.googlecode.objectify.annotation.Unindexed;

@Unindexed
@Cached(expirationSeconds = 600)
/**
 * A profile over time (one {@link PlanProfileEntry} per day) for a given resource
 */
public class PlanDailyProfile extends DailyProfile_LinkedList<PlanProfileEntry> {
	protected static final Logger logger = Logger.getLogger(PlanDailyProfile.class.getName());
	private static final long serialVersionUID = 1L;
	private Key<Resource> resourceKey;

	@Serialized
	private LinkedList<PlanProfileEntry> profile = new LinkedList<PlanProfileEntry>();

	public PlanDailyProfile() {
		super();
	}

	public PlanDailyProfile(Resource resource) {
		super();
		setResource(resource);
	}

	@SuppressWarnings("unchecked")
	private void setResource(Resource resource) {
		if (resource == null) {
			logger.warning("resource to null in PlanDailyProfile");
			resourceKey = null;
			return;
		}

		try {
			resourceKey = (Key<Resource>) resource.getKey();
		} catch (PersistenceReferenceException e) {
			logger.warning("resource to null in PlanDailyProfile");
			resourceKey = null;
		}

	}

	@Override
	protected LinkedList<PlanProfileEntry> profile() {
		return profile;
	}

	public Key<Resource> getResourceKey() {
		return resourceKey;
	}

}
